package com.linkoog.devtools.generate.singleton;

import com.linkoog.devtools.generate.singleton.builder.BaseBuilder;
import com.linkoog.devtools.generate.singleton.builder.EnumPatternBuilder;
import com.linkoog.devtools.generate.singleton.builder.HungryPatternBuilder;
import com.linkoog.devtools.generate.singleton.builder.InnerClassPatternBuilder;
import com.linkoog.devtools.generate.singleton.builder.LazyPatternBuilder;
import com.linkoog.devtools.generate.singleton.builder.ThreadSafePatternBuilder;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * 选择单例实现模式
 */
public class PatternPickerDialog extends JDialog {
    private JPanel mContentPane;
    private JButton mButtonOK;
    private JButton mButtonCancel;
    private JList mListPattern;

    private final BuildContext buildContext;

    public PatternPickerDialog(@NotNull BuildContext buildContext) {
        this.buildContext = buildContext;
        setContentPane(mContentPane);
        setModal(true);
        getRootPane().setDefaultButton(mButtonOK);
        setTitle("Choose a Pattern");

        mButtonOK.addActionListener(e -> onOK());
        mButtonCancel.addActionListener(e -> onCancel());

        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        mContentPane.registerKeyboardAction(e -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        mListPattern.setSelectedIndex(0);
        mListPattern.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);




        mListPattern.addKeyListener(new KeyListener() {
            int preIndex = -1;

            @Override
            public void keyTyped(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                preIndex = mListPattern.getSelectedIndex();
            }

            @Override
            public void keyReleased(KeyEvent e) {
                int selectedIndex = mListPattern.getSelectedIndex();
                if (preIndex == selectedIndex) {
                    if (selectedIndex == 0) {
                        if (e.getKeyCode() == KeyEvent.VK_UP) {
                            mListPattern.setSelectedIndex(4);
                        }
                    } else if (selectedIndex == 4) {
                        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                            mListPattern.setSelectedIndex(0);
                        }
                    }
                }
            }
        });
        setBounds(new Rectangle(300, 200));
        setLocationRelativeTo(null);
    }

    private void onOK() {
        dispose();
        BaseBuilder builder = getBuilder(mListPattern.getSelectedIndex());
        builder.build(buildContext.getProject(), buildContext.getEditor(), buildContext.getPsiClass());
    }



    private void onCancel() {
        dispose();
    }

    private BaseBuilder getBuilder(int index){
        BaseBuilder builder = null;
        switch (index) {
            case 0:
                builder = new EnumPatternBuilder();
                break;
            case 1:
                builder = new LazyPatternBuilder();
                break;
            case 2:
                builder = new HungryPatternBuilder();
                break;
            case 3:
                builder = new ThreadSafePatternBuilder();
                break;
            case 4:
                builder = new InnerClassPatternBuilder();
                break;
        }
        return builder;
    }
}
